package cn.lg.soar.general.controller;

import cn.lg.soar.general.api.constant.ModuleConst;
import cn.lg.soar.general.api.enums.ThumbnailSizeEnum;
import cn.lg.soar.general.service.FileAccessService;
import cn.lg.soar.mvc.util.ResponseUtils;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.S3Object;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Api(tags = "文件访问控制")
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/file")
public class FileAccessController {

    @Autowired
    private FileAccessService service;

    @ApiOperation("无需登录的临时路径")
    @GetMapping("anon/{sign}/{bucketName}/{fileInfo}")
    public void anonTemp(
            @ApiParam @PathVariable String sign,
            @ApiParam @PathVariable String bucketName,
            @ApiParam @PathVariable String fileInfo,
            @ApiParam @RequestParam(value = "s", required = false) ThumbnailSizeEnum size,
            HttpServletResponse response
    ) {
        S3Object s3Object = service.temporaryUrlAccess(sign, bucketName, fileInfo, size);
        ObjectMetadata objectMetadata = s3Object.getObjectMetadata();
        response.setContentType(objectMetadata.getContentType() + "; charset=UTF-8");
        ResponseUtils.responseFile(objectMetadata.getContentLength(), s3Object.getObjectContent(), null);
    }

    @ApiOperation("无需登录的临时路径")
    @GetMapping("anon/{sign}/{bucketName}/{fileInfo}/{filename}")
    public void anonTemp(
            @ApiParam @PathVariable String sign,
            @ApiParam @PathVariable String bucketName,
            @ApiParam @PathVariable String fileInfo,
            @ApiParam @RequestParam(value = "s", required = false) ThumbnailSizeEnum size,
            @ApiParam @PathVariable String filename
    ) {
        S3Object s3Object = service.temporaryUrlAccess(sign, bucketName, fileInfo, size);
        ObjectMetadata objectMetadata = s3Object.getObjectMetadata();
        ResponseUtils.responseFile(objectMetadata.getContentLength(), s3Object.getObjectContent(), filename);
    }

    @ApiOperation("需要登录的临时路径")
    @GetMapping("auth/{sign}/{bucketName}/{fileInfo}")
    public void authTemp(
            @ApiParam @PathVariable String sign,
            @ApiParam @PathVariable String bucketName,
            @ApiParam @PathVariable String fileInfo,
            @ApiParam @RequestParam(value = "s", required = false) ThumbnailSizeEnum size,
            HttpServletResponse response
    ) {
        S3Object s3Object = service.temporaryUrlAccess(sign, bucketName, fileInfo, size);
        ObjectMetadata objectMetadata = s3Object.getObjectMetadata();
        response.setContentType(objectMetadata.getContentType() + "; charset=UTF-8");
        ResponseUtils.responseFile(objectMetadata.getContentLength(), s3Object.getObjectContent(), null);
    }

    @ApiOperation("需要登录的临时路径")
    @GetMapping("auth/{sign}/{bucketName}/{fileInfo}/{filename}")
    public void authTemp(
            @ApiParam @PathVariable String sign,
            @ApiParam @PathVariable String bucketName,
            @ApiParam @PathVariable String fileInfo,
            @ApiParam @RequestParam(value = "s", required = false) ThumbnailSizeEnum size,
            @ApiParam @PathVariable String filename
    ) {
        S3Object s3Object = service.temporaryUrlAccess(sign, bucketName, fileInfo, size);
        ObjectMetadata objectMetadata = s3Object.getObjectMetadata();
        ResponseUtils.responseFile(objectMetadata.getContentLength(), s3Object.getObjectContent(), filename);
    }

    @ApiOperation("固定访问路径")
    @GetMapping("fixed/{secret}/{fileId}")
    public void fixedSecret(
            @ApiParam @PathVariable String secret,
            @ApiParam @PathVariable long fileId,
            @ApiParam @RequestParam(value = "s", required = false) ThumbnailSizeEnum size,
            HttpServletResponse response
    ) {
        S3Object s3Object = service.fixedSecretAccess(secret, fileId, size);
        ObjectMetadata objectMetadata = s3Object.getObjectMetadata();
        response.setContentType(objectMetadata.getContentType() + "; charset=UTF-8");
        ResponseUtils.responseFile(objectMetadata.getContentLength(), s3Object.getObjectContent(), null);
    }

    @ApiOperation("固定访问路径")
    @GetMapping("fixed/{secret}/{fileId}/{filename}")
    public void fixedSecret(
            @ApiParam @PathVariable String secret,
            @ApiParam @PathVariable long fileId,
            @ApiParam @RequestParam(value = "s", required = false) ThumbnailSizeEnum size,
            @ApiParam @PathVariable String filename
    ) {
        S3Object s3Object = service.fixedSecretAccess(secret, fileId, size);
        ObjectMetadata objectMetadata = s3Object.getObjectMetadata();
        ResponseUtils.responseFile(objectMetadata.getContentLength(), s3Object.getObjectContent(), filename);
    }

}
